package com.lzxuni.modules.enclosure.controller;

import com.alibaba.fastjson.JSON;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.constant.LogConstant;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.enclosure.service.EnclosureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/LR_EnclosureModule/Enclosure")
public class EnclosureController extends BaseController {

    @Autowired
    private EnclosureService enclosureService;


    @RequestMapping("/Index")
    @SysLog(categoryId = LogConstant.VIEWID, module = "附件功能", operateType = LogConstant.VIEW)
    public ModelAndView Form() {
        ModelAndView mv = new ModelAndView("/LR_EnclosureModule/Enclosure/index");
        return mv;
    }

    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson, Integer categoryId) throws ParseException {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        FileEntity fileEntity = new FileEntity();
        if (StringUtils.isNotEmpty(queryJson)) {
            String str = queryJson.replaceAll("&quot", "\"");
            com.alibaba.fastjson.JSONObject jsStr = com.alibaba.fastjson.JSONObject.parseObject(str);

            if (StringUtils.isNotEmpty(jsStr.getString("StartTime"))) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                fileEntity.setStartTime(sdf.parse(jsStr.getString("StartTime")));
            }else{}
            if (StringUtils.isNotEmpty(jsStr.getString("EndTime"))) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                fileEntity.setEndTime(sdf.parse(jsStr.getString("EndTime")));
            }
            if (StringUtils.isNotEmpty(jsStr.getString("type"))) {
                fileEntity.setType(jsStr.getString("type"));
            }
            if (StringUtils.isNotEmpty(jsStr.getString("ywType"))) {
                fileEntity.setYwType(jsStr.getString("ywType"));
            }
        }
        PageData pageData = getPageData(enclosureService.queryPage(pageParameter, fileEntity));
        return R.ok().put("data", pageData);
    }

    @RequestMapping("/Form")
    public ModelAndView Forma(String id) {
        ModelAndView mv = new ModelAndView("/LR_EnclosureModule/Enclosure/form");
        return mv;
    }

    @RequestMapping("/SaveForm")
    @SysLog(categoryId = LogConstant.OPERATEID, module = "附件功能", operateType = LogConstant.OPERATE)
    public Object insertDo(String keyValue,FileEntity fileEntity) {
        if (StringUtils.isNotEmpty(keyValue)) {
            fileEntity.setYwId("999999");

        } else {

        }
        return R.ok().put("data", "保存成功！");
    }

}
